/* Usage:
 * change(str, flag);
 * change to the second step and display the in str defined input type
 * parameter:
 * str - string specification of the input type ( manual, bibtex, api )
 * flag - 0 for not change step 1 for change to next step 2 for change to prev step
 * return:
 * -
 */
function change(str, flag) {
    if (flag === undefined) {
        flag = 1;
    }
    if (str === "manual") {
        document.getElementById("manual_form").style.display = "block";
        if (flag === 1)
            $('#MyWizard').wizard('next');
        else if (flag === 2)
            $('#MyWizard').wizard('previous');
    } else if (str === "bibtex") {
        document.getElementById("bibtex_form").style.display = "block";
        if (flag === 1)
            $('#MyWizard').wizard('next');
        else if (flag === 2)
            $('#MyWizard').wizard('previous');
    } else if (str === "api") {
        document.getElementById("api_form").style.display = "block";
        if (flag === 1)
            $('#MyWizard').wizard('next');
        else if (flag === 2)
            $('#MyWizard').wizard('previous');
    } else {
        document.getElementById("api_form").style.display = "none";
        document.getElementById("manual_form").style.display = "none";
        document.getElementById("bibtex_form").style.display = "none";

        $('#MyWizard').wizard('previous');

        document.getElementsByName("title")[0].style.border = "";
        document.getElementsByName("year")[0].style.border = "";
        document.getElementsByName("author")[0].style.border = "";
        document.getElementsByName("api")[0].style.border = "";
        document.getElementsByName("bib_in")[0].style.border = "";
    }
}

/*
 * Usage: return IsEmpty(str); check if specific inputs are empty and highlight
 * them parameter: str - string with the specification of the input ( manual,
 * bib, api ) return: true - if all inputs are filled in false - if not and they
 * get highlighted
 */
function IsEmpty(str) {
    var ret = true;
    if (str === "manual") {
        var patt = new RegExp("\d{1,4}");
        if (document.getElementsByName("title")[0].value === "") {
            document.getElementsByName("title")[0].style.border = "1px solid red";
            ret = false;
        } else {
            document.getElementsByName("title")[0].style.border = "";
        }
        if (document.getElementsByName("year")[0].value === "") {
            document.getElementsByName("year")[0].style.border = "1px solid red";
            ret = false;
        } else if (patt.test(document.getElementsByName("year")[0].value)) {
            document.getElementsByName("year")[0].style.border = "1px solid red";
            ret = false;
        } else {
            document.getElementsByName("year")[0].style.border = "";
        }
        if (document.getElementsByName("author")[0].value === "") {
            document.getElementsByName("author")[0].style.border = "1px solid red";
            ret = false;
        } else {
            document.getElementsByName("author")[0].style.border = "";
        }
        if (document.getElementsByName("note")[0].value !== "") {
            ret = true;
        }
    } else if (str === "api") {
        document.getElementsByName("select_api")[0].value = document
                .getElementById("selected_api").innerHTML;
        if (document.getElementsByName("api")[0].value === "") {
            document.getElementsByName("api")[0].style.border = "1px solid red";
            ret = false;
        }
    } else if (str === "bib") {
        if (document.getElementsByName("bib_in")[0].value === "") {
            document.getElementsByName("bib_in")[0].style.border = "1px solid red";
            ret = false;
        }
    }
    return ret;
}

/*
 * Usage: return checkValid(); checks if any of the radio buttons is not a blank
 * info struct paramter: - return: true - any none blank info struct radio
 * button is checked false - all blank info structs radio buttons are checked
 */
function checkValid() {
    var ret = true;
    var zma = false;
    var arx = false;
    var doi = false;
    var man = false;
    var note = false;

    if (document.querySelector('input[name="radiozma"]:checked') != null) {
        if (document.querySelector('input[name="radiozma"]:checked').value
                .search("_blank") != -1) {
            zma = false;
        } else {
            zma = true;
        }
    }

    if (document.querySelector('input[name="radioarx"]:checked') != null) {
        if (document.querySelector('input[name="radioarx"]:checked').value
                .search("_blank") != -1) {
            arx = false;
        } else {
            arx = true;
        }
    }

    if (document.querySelector('input[name="radioman"]:checked') != null) {
        if (document.querySelector('input[name="radioman"]:checked').value
                .search("_blank") != -1) {
            doi = false;
        } else {
            doi = true;
        }
    }

    if (document.querySelector('input[name="radiodoi"]:checked') != null) {
        if (document.querySelector('input[name="radiodoi"]:checked').value
                .search("_blank") != -1) {
            man = false;
        } else {
            man = true;
        }
    }

    if (document.getElementById("add_note") != null) {
        if (document.getElementById("add_note").value == null
                || document.getElementById("add_note").value == "") {
            note = false;
        } else {
            note = true;
        }
    }

    if (!zma && !arx && !doi && !man && !note) {
        ret = false;
        document.getElementById("selection_error").innerHTML = "Error: you have to select one to proceed or go back to correct your errors.";
    }

    return ret;
}

/* Usage:
 * onclick='close_popup();'
 * used to send data to the parent/opening window function 'get_ref'
 * parameter:
 * -
 * return:
 * -
 */
function close_popup() {
    var text = document.getElementById("ref_id").innerHTML + " - "
            + document.getElementById("ref_title").innerHTML + " by "
            + document.getElementById("ref_author").innerHTML;
    window.opener.get_ref(text);
    self.close();
}